package com.fuda.trade.model

import com.fuda.trade.model.TradeOrder.Companion
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json

@Serializable
data class TradeHolding(
    @SerialName("id")
    val id: FudaThing?,
    @SerialName("holding_id")val holdingId: String?=null,

    val data: TradeHoldingData?,
    @SerialName("last_updated")val lastUpdated: String?=null
){

    // Secondary constructor for creating a TradeOrder with only id and data
    constructor(id: FudaThing, data: TradeHoldingData) :this(
        id = id,
        holdingId = null,
        data = data,
        lastUpdated = null
    )


    companion object {
        private val json = Json {
            encodeDefaults = true
            ignoreUnknownKeys=true
        }

        // copied from trade/mod.rs
        const val TRADE_HOLDING_ID_MONEY_CNY = "MY.HOLDING.MONEY.CNY"
        const val TRADE_HOLDING_ID_MONEY_HKD = "MY.HOLDING.MONEY.HKD";
        const val TRADE_HOLDING_ID_MONEY_USD = "MY.HOLDING.MONEY.USD";

        fun serialize(tradeOrder: TradeHolding): String {
            return json.encodeToString(tradeOrder)
        }

        fun deserialize(jsonStr: String): TradeHolding {
            return json.decodeFromString(jsonStr)
        }


        fun deserializeList(jsonStr: String): List<TradeHolding> {
            return json.decodeFromString(jsonStr)
        }
    } 
}